
var MongoClient = require('mongodb').MongoClient;

function dbClient(host, db) {
	if(host){
		this._host = host;
	}else{
		
	}
	
	if(db){
		this._db = db;
	}else{
		
	}

};

dbClient.prototype.insert = function insert(collection, info, callback) {
	console.log("insert " + this._host + this._db);
	MongoClient.connect(this._host + this._db, function(err, db) {
		if (!err) {
			console.log('db is connected!');
			this._collection = db.collection(collection);
			this._collection.insert(info, function(err, result){
				if(!err){
					callback(err,result);
					db.close();
				}else{
					console.log(err);
					callback(err,result);
					db.close();
				}
			});
		} else {
			console.log(err);
		}
	});
};

dbClient.prototype.findOne = function(collection, query, callback){
	console.log("findOne " + this._host + this._db);
	MongoClient.connect(this._host + this._db, function(err1, db) {
		if (!err1) {
			console.log('db is connected!');
			this._collection = db.collection(collection);

			this._collection.findOne(query, function(err2, result) {
				callback(err1, err2, result);
				db.close();
			});
		} else {
			console.log(err1);
			callback(err1, db);
		}
	});
};

dbClient.prototype.remove = function(selector, callback){
	
};


module.exports = dbClient;